package chaoyue.study.thread;

/**
 * 使用interrupt终止阻塞
 */
public class ThreadInterrupt {
    public static void main(String[] args) throws Exception {
        Thread t = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " ---> begin");
            try {
                // sleep使线程进入阻塞态
                Thread.sleep(1000 * 60 * 60);
            } catch (InterruptedException e) {
                System.out.println("线程sleep被中断");
            }
            // 阻塞被打断后会抛出InterruptedException异常，如果处理了这个异常 子进程会继续执行
            // 比如下面这行代码会继续打印
            System.out.println(Thread.currentThread().getName() + " ---> end");
        });
        t.start();
        Thread.sleep(3000);
        t.interrupt(); // 终止线程的阻塞状态，被中止的线程会抛出InterruptedException
        System.out.println(Thread.currentThread().getName() + " ---> end");
    }
}
